package com.wm.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wm.bean.City;
import com.wm.bean.Shopkeeper;
import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.dao.ShopkeeperDAO;
import com.wm.dao.UserDAO;

/**
 * @author yangbo
 *
 * 2012-12-26
 */
public class ShopkeeperHibernateDAO extends HibernateDaoSupport implements ShopkeeperDAO {
	private static final String LOAD_BY_ID = "from Shopkeeper where id = ?";
	private static final String LOAD_BY_USER_NAME = "from Shopkeeper where userName = ?";
	private static final String REMOVE_BY_ID = "delete Shopkeeper where id = ?";

	public Shopkeeper saveShopkeeper(Shopkeeper sk){
		this.getHibernateTemplate().save(sk);
		return sk;
	}

	public Shopkeeper updateShopkeeper(Shopkeeper sk){
		this.getHibernateTemplate().update(sk);
		return sk;
	}
	
	public void removeShopkeeperById(final long id){
		getHibernateTemplate().execute(new HibernateCallback() {
	        public Object doInHibernate(Session s) throws HibernateException, SQLException {
	          Query query = s.createQuery(REMOVE_BY_ID);
	          query.setLong(0, id);
	          query.executeUpdate();
	          return null;
	        }
	      });
	}
	
	public Shopkeeper findShopkeeperById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Shopkeeper) l.get(0);
		}
	}
	
	public Shopkeeper findShopkeeperByUserName(String userName){
		List l = this.getHibernateTemplate().find(LOAD_BY_USER_NAME,userName);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Shopkeeper) l.get(0);
		}
	}
	/**
	 * 根据参数获取Shopkeeper对象列表
	 * 
	 * @param status
	 * 			int
	 * @param orderby
	 * @param ascOrDesc
	 * @param firstResult
	 * @param maxResults
	 * @return List
	 * 			List
	 */
	public List findShopkeepersByUserNameStatus(String userName,int status,String orderby,int ascOrDesc,final int firstResult,final int maxResults){
		StringBuffer sb = new StringBuffer();
		sb.append("from Shopkeeper where 1=1");
		if(StringUtils.isNotBlank(userName)){
			sb.append(" and userName like '%");
			sb.append(userName);
			sb.append("%'");
		}
		if (status != -1) {
			sb.append(" and status=");
			sb.append(status);
		}
		if (StringUtils.isNotBlank(orderby)) {
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" order by " + orderby+" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" order by " + orderby+" desc");
			}
		}
		final String sql = sb.toString();
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(sql);
				query.setFirstResult(firstResult);
				query.setMaxResults(maxResults);
				List list = query.list();
				return list;
			}
		});
	}
	
	/**
	 * 根据参数获取Shopkeeper对象个数
	 * @param status
	 * 			int
	 * @return lonh
	 * 			long
	 */
	public long getShopkeeperNumByUserNameStatus(String userName,int status){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(id) from Shopkeeper where 1=1");
		if(StringUtils.isNotBlank(userName)){
			sb.append(" and userName like '%");
			sb.append(userName);
			sb.append("%'");
		}
		if (status != -1) {
			sb.append(" and status=");
			sb.append(status);
		}
		List l = this.getHibernateTemplate().find(sb.toString());
		
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return (Long) l.get(0);
		}
	}
}
